package cn.butcher21.dao;

import cn.butcher21.utils.DBUtil;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DBDao {
    public static void checkTableExist() {
        try {
            DBUtil dbUtil = new DBUtil();
            PreparedStatement ps = dbUtil.getPs("SHOW TABLES");
            ResultSet rs = dbUtil.getRs(ps.executeQuery());
            List<String> tables = new ArrayList<String>();
            while (rs.next()) {
                tables.add(rs.getString(1));
            }
            dbUtil.close();
            if (!tables.containsAll(Arrays.asList("T_GOODS", "T_SALE", "T_USER"))) {
                System.out.println("=========== 检测到程序所需表不存在，正在为您生成表，请稍后 =================");
                // 创建表
                initTable();
                System.out.println("=========== 表已生成，即将运行程序 ===========");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void initTable() {
        try {
            DBUtil dbUtil = new DBUtil();
            String sql = "create table T_GOODS\n" +
                    "(\n" +
                    "    G_ID    VARCHAR(255) not null,\n" +
                    "    G_NAME  VARCHAR(255),\n" +
                    "    G_COUNT INT,\n" +
                    "    G_PRICE DOUBLE,\n" +
                    "    G_COST  DOUBLE,\n" +
                    "    U_NAME  VARCHAR(255),\n" +
                    "    constraint T_GOODS_PK\n" +
                    "        primary key (G_ID)\n" +
                    ");";
            PreparedStatement ps = dbUtil.getPs(sql);
            ps.execute();

            sql = "create table T_SALE\n" +
                    "(\n" +
                    "    S_ID    VARCHAR(255) not null,\n" +
                    "    G_ID    VARCHAR(255),\n" +
                    "    S_COUNT INT,\n" +
                    "    U_NAME  VARCHAR(255),\n" +
                    "    constraint T_SALE_PK\n" +
                    "        primary key (S_ID)\n" +
                    ");";
            ps = dbUtil.getPs(sql);
            ps.execute();

            sql = "create table T_USER\n" +
                    "(\n" +
                    "    U_NAME     VARCHAR(255) not null,\n" +
                    "    U_PASSWORD VARCHAR(255),\n" +
                    "    constraint T_USER_PK\n" +
                    "        primary key (U_NAME)\n" +
                    ");";
            ps = dbUtil.getPs(sql);
            ps.execute();
            // 默认用户
            sql = "INSERT INTO PUBLIC.T_USER (U_NAME, U_PASSWORD) VALUES ('admin', '202cb962ac59075b964b07152d234b70')";
            ps = dbUtil.getPs(sql);
            ps.execute();

            dbUtil.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
